using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using ErkerTech.DeltaScope.App.Models;
using ErkerTech.DeltaScope.Engine;
using ErkerTech.DeltaScope.View;
using ErkerTech.DeltaScope.View.Controls;
using ErkerTech.DeltaScope.View.Models;

namespace ErkerTech.DeltaScope.App.Controls
{
    public class DeltaScopePage : TabPage 	
    {
        #region Private Declarations
        private readonly SwirlController _swirlController;
        private readonly List<SwirlBlock> _blockList;
        private readonly IContainer _components;
        private SwirlDisplay _swirlDisplay;
        private BlockHistogram _blockHistogram;
        private Splitter _splitter;
        #endregion

        #region Constructor
        public DeltaScopePage()
        {
            // This call is required by the Windows.Forms Form Designer.
            InitializeComponent();

            DeltaScopeApp.SwirlFontChanged += ((sender, e) =>
                                                   {
                                                       _swirlController.Font = DeltaScopeApp.SwirlFont;
                                                       _swirlDisplay.Refresh();
                                                   });
            DeltaScopeApp.WordWrapChanged += ((sender, e) =>
                                                  {
                                                      _swirlController.WordWrap = DeltaScopeApp.WordWrap;
                                                      _swirlDisplay.Refresh();
                                                  });
            DeltaScopeApp.SwirlRegionVisibleChanged += ((sender, e) =>
                                                            {
                                                                _swirlDisplay.ShowSwirlRect = DeltaScopeApp.SwirlRegionVisible;
                                                            });

            _blockList = new List<SwirlBlock>();
            _swirlController = new SwirlController(_swirlDisplay, _blockList);
        }
        #endregion

        #region Public Methods
        public void Bind(IList<DeltaBlock> deltaBlockList)
        {
            // Loop through all the blocks...
            foreach (var loBlock in deltaBlockList)
                _blockList.Add(new SwirlBlock(loBlock));

            // Bind the controller to the control...			
            _swirlController.Bind();	
			
            //Bind the Histogram to the same BlockList...
            _blockHistogram.Blocks = _blockList;
        }
        #endregion

        #region .NET Designer stuff
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(_components != null) _components.Dispose();
            }
            base.Dispose( disposing );
        }

        #region Component Designer generated code
        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this._swirlDisplay = new ErkerTech.DeltaScope.View.Controls.SwirlDisplay();
            this._splitter = new System.Windows.Forms.Splitter();
            this._blockHistogram = new ErkerTech.DeltaScope.View.Controls.BlockHistogram();
            this.SuspendLayout();
            // 
            // _swirlDisplay
            // 
            this._swirlDisplay.BackColor = System.Drawing.SystemColors.Window;
            this._swirlDisplay.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this._swirlDisplay.Dock = System.Windows.Forms.DockStyle.Fill;
            this._swirlDisplay.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel, ((byte)(0)));
            this._swirlDisplay.ForeColor = System.Drawing.SystemColors.ControlText;
            this._swirlDisplay.Location = new System.Drawing.Point(0, 52);
            this._swirlDisplay.Name = "_swirlDisplay";
            this._swirlDisplay.ShowSwirlRect = false;
            this._swirlDisplay.Size = new System.Drawing.Size(392, 216);
            this._swirlDisplay.SwirlAreaWidth = 80;
            this._swirlDisplay.TabIndex = 3;
            this._swirlDisplay.TabStop = false;
            this._swirlDisplay.WordWrap = false;
            // 
            // _splitter
            // 
            this._splitter.Dock = System.Windows.Forms.DockStyle.Top;
            this._splitter.Location = new System.Drawing.Point(0, 48);
            this._splitter.Name = "_splitter";
            this._splitter.Size = new System.Drawing.Size(392, 4);
            this._splitter.TabIndex = 7;
            this._splitter.TabStop = false;
            // 
            // _blockHistogram
            // 
            this._blockHistogram.BackColor = System.Drawing.Color.DimGray;
            this._blockHistogram.Blocks = null;
            this._blockHistogram.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this._blockHistogram.Dock = System.Windows.Forms.DockStyle.Top;
            this._blockHistogram.ForeColor = System.Drawing.Color.DarkGray;
            this._blockHistogram.LeftColor = System.Drawing.Color.CornflowerBlue;
            this._blockHistogram.Location = new System.Drawing.Point(0, 0);
            this._blockHistogram.Name = "_blockHistogram";
            this._blockHistogram.RightColor = System.Drawing.Color.Red;
            this._blockHistogram.Size = new System.Drawing.Size(392, 48);
            this._blockHistogram.TabIndex = 5;
            this._blockHistogram.TabStop = false;
            this._blockHistogram.Click += new System.EventHandler(this.BlockHistogramClickHandler);
            // 
            // DeltaScopePage
            // 
            this.Controls.Add(this._swirlDisplay);
            this.Controls.Add(this._splitter);
            this.Controls.Add(this._blockHistogram);
            this.Size = new System.Drawing.Size(392, 272);
            this.ResumeLayout(false);

        }
        #endregion
        #endregion

        #region local event handlers
        private void BlockHistogramClickHandler(object sender, EventArgs e)
        {
            var lnCurrentBlock = _blockHistogram.CurrentBlockIndex;
            if (lnCurrentBlock >= 0) _swirlController.JumpToBlock(lnCurrentBlock);
        }
        #endregion
    }
}